package com.example.cet4try.activity;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.Utils.WordData;
import com.example.cet4try.myDialog.WordDetailsDialog;

import java.util.ArrayList;

public class ActivityWordSpelling extends BaiduTTSActivity {

//    private String[] data;
    private WordData wordData;
    private String english;
    private ArrayList<String> english_s = new ArrayList<String>();
    private TextView spelling_show;
    private int spell_length;
//    private Button button1,button2,button3,button4,button5,button6,button7,button8,button9;
    private Button[] buttons = new Button[6];
    private ArrayList<String> word_s = new ArrayList<String>();
    private WordDetailsDialog wordDetailsDialog;
    private boolean endflag = false,wrongFlag = false;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_spelling);

        initView();

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");

        int id = Tools.MyRandom(1,4493);
        wordData = Tools.souID(id);
        Tools.AddUserData(username,wordData.getMid(),"learned");

        TextView chinese_show = findViewById(R.id.chinese_show1);
        wordDetailsDialog = new WordDetailsDialog("拼写测试");
        wordDetailsDialog.setPlaySound(new WordDetailsDialog.PlaySound() {
            @Override
            public void speak_d() {
                speak();
            }
        });

        Button btn_rs = findViewById(R.id.button5);
        btn_rs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.AddUserData(username,wordData.getMid(),"already");
                reStart();
            }
        });

        english = wordData.getEnglish();
        chinese_show.setText(wordData.getChinese().replaceAll("#","\n"));
        if (english.length() <= 6){
            for (int i = 0;i < english.length();i++){
                word_s.add(english.substring(i,i+1));
            }
        }
        else {
            int n,k;
            for (k = 2;;k++){
                n = english.length()/k;
                if (english.length() % k != 0)
                    n++;
                if (n <= 6)
                    break;
            }
            for (int i = 0;word_s.size() < n;i += k){
                if (word_s.size() < n-1)
                    word_s.add(english.substring(i,i+k));
                else
                    word_s.add(english.substring(i));
            }
        }
        spell_length = word_s.size();
        if (spell_length < 4){
            LinearLayout btn_46 = findViewById(R.id.btn_46);
            btn_46.setVisibility(View.GONE);
        }

        randomList();
        StringBuilder word = new StringBuilder();
        for (int i = 0;i < spell_length;i++){
            Button button = buttons[i];
            button.setVisibility(View.VISIBLE);
            button.setText(word_s.get(i));
            button.setOnClickListener(new button_spelling_onclick());
            word.append("_");
        }
        spelling_show.setText(word.toString());

        Button next = findViewById(R.id.next_pin);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reStart();
            }
        });

        Button langdu = findViewById(R.id.langdux);
        langdu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speak();
            }
        });


    }

    private void initView(){
        spelling_show = findViewById(R.id.spelling_show);
        buttons[0] = findViewById(R.id.button1);
        buttons[1] = findViewById(R.id.button2);
        buttons[2] = findViewById(R.id.button3);
        buttons[3] = findViewById(R.id.button4);
        buttons[4] = findViewById(R.id.button5);
        buttons[5] = findViewById(R.id.button6);

    }

    private class button_spelling_onclick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if (endflag)
                return;
            AddTimes();

            Button button = (Button)v;

            String s = button.getText().toString();
            int index = english_s.lastIndexOf(s);
            int color = button.getCurrentTextColor();
            //String c = Integer.toHexString(color);
            if (color == 0xff333333){
                english_s.add(s);
                button.setTextColor(0xffffffff);
                button.setBackground(getResources().getDrawable(R.drawable.button_spelling2));
            }
            else {
                english_s.remove(index);
                button.setTextColor(0xff333333);
                button.setBackground(getResources().getDrawable(R.drawable.button_spelling1));
            }

            StringBuilder s2 = new StringBuilder();
            for (int i = 0;i<english_s.size();i++){
                s2.append(english_s.get(i));
            }
            for (int i = english_s.size();i < spell_length;i++)
                s2.append("_");

            String spelling_text = s2.toString();

            spelling_show.setText(spelling_text);

            if (english_s.size() == spell_length){

//                Drawable noicon = getResources().getDrawable(R.drawable.no);
//                noicon.setBounds(0,0,90,90);
                if (english.equals(spelling_text)){
                    endflag = true;
                    if (!wrongFlag)
                        AddRight();
                    Drawable yesicon = getResources().getDrawable(R.drawable.yes);
                    yesicon.setBounds(0,0,90,90);
                    spelling_show.setCompoundDrawables(null, null, yesicon, null);
                    wordDetailsDialog.setData(wordData);
                    wordDetailsDialog.show(getSupportFragmentManager(),"WordDetailsDialog");
                }
                else{
                    wrongFlag = true;
                    String t = "<font color='#D0021B'>" + spelling_text + "</font>";
                    spelling_show.setText(Html.fromHtml(t));
                    Animation shake = AnimationUtils.loadAnimation(ActivityWordSpelling.this,R.anim.shake2);
                    spelling_show.startAnimation(shake);
                    Tools.AddUserData(username,wordData.getMid(),"wrong");
                }

            }

        }
    }

    private void speak() {
        super.speak(wordData.getEnglish());
    }

    private void randomList(){
        ArrayList<String> list_new = new ArrayList<String>();
        while (word_s.size() > 0){
            int r = Tools.MyRandom(0,word_s.size());
            list_new.add(word_s.get(r));
            word_s.remove(r);
        }
        word_s = list_new;
    }

}
